<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <span class="cu-font">基础信息</span>
            <a-divider style="margin: 16px 0" />
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="number">
              <a-input v-model="model.number" placeholder="请输入编号" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <!--          <a-col :span="24">
                      <a-form-model-item label="客户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerId">
                        <j-search-select-tag v-model="model.customerId" dict="biz_customer,name,id" />
                      </a-form-model-item>
                    </a-col>-->
          <!--          <a-col :span="24">
                      <a-form-model-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contactsIds">
                        <j-multi-select-tag type="list_multi" v-model="model.contactsIds" dictCode="biz_contacts,name,id"
                                            placeholder="请选择联系人" />
                      </a-form-model-item>
                    </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="回款金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="collectionAmount">
              <a-input-number v-model="model.collectionAmount" placeholder="请输入回款金额"
                              class="w100"></a-input-number>

            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="归属人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="owner">
              <!--              <a-input v-model="model.owner"></a-input>-->
              <j-select-user-by-dep v-model="model.owner" :multi="true" placeholder="请选择员工" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="回款日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="collectionDate">
              <j-date placeholder="请选择回款日期" v-model="model.collectionDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="收款账户" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="collectionAccount">
              <j-dict-select-tag type="list" v-model="model.collectionAccount" dictCode="collection_account"
                                 placeholder="请选择回款账户" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="支付方式" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="paymentMethod">
              <j-dict-select-tag type="list" v-model="model.paymentMethod" dictCode="collection_payment_method"
                                 placeholder="请选择支付方式" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <span class="cu-font">扩展信息</span>
            <a-divider style="margin: 16px 0" />
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remrak">
              <!--              <j-editor v-model="model.remrak" />-->
              <a-textarea
                v-model="model.remrak"
                placeholder="请输入备注"
                :auto-size="{ minRows: 3, maxRows: 5 }"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="files">
              <j-upload v-model="model.files"></j-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="负责人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="directorIds">
              <j-select-user-by-dep v-model="model.directorIds" :multi="true" placeholder="请选择员工" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="协同人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="collaboratorIds">
              <j-select-user-by-dep v-model="model.collaboratorIds" :multi="true" placeholder="请选择员工" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import JSelectUserByDep from '@/components/jeecgbiz/JSelectUserByDepCustom'
import moment from 'moment'

export default {
  name: 'PaymentCollectionForm',
  components: {
    JSelectUserByDep
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      canBeAdd: 0,
      model: {
        number: '',
        collectionAmount: 0,
        collectionDate: ''
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        number: [
          { required: true, message: '请输入编号!' }
        ],
        collectionAmount: [
          { required: true, message: '请输入回款金额!' },
          {
            validator: (rule, value, callback) => {
              if (Number(value) > this.canBeAdd) {
                callback(new Error(`回款金额不能超过${this.canBeAdd}`))
              } else {
                callback()
              }
            },
            trigger: ['blur']
          }
        ],
        owner: [
          { required: true, message: '请选择归属人!' }
        ],
        collectionDate: [
          { required: true, message: '请选择回款日期!' }
        ],
        collectionAccount: [
          { required: true, message: '请选择收款账户!' }
        ],
        directorIds: [
          { required: true, message: '请选择负责人!' }
        ]
      },
      url: {
        getNumber: '/sys/common/getNumber',
        add: '/payment/paymentCollection/add',
        edit: '/payment/paymentCollection/edit',
        queryById: '/payment/paymentCollection/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    moment,
    add() {
      this.edit(this.modelDefault)
      getAction(this.url.getNumber).then(res => {
        console.log(res)
        this.model.number = res.result
      })
      //赋值回款日期为当前日期
      this.model.collectionDate = this.moment().format('YYYY-MM-DD')
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      that.confirmLoading = true
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = false
          // that.confirmLoading = true
          that.$emit('ok', this.model)

        } else {
          that.confirmLoading = false
        }

      })
    },
    passON(contractAmount) {
      //回款金额赋值
      this.model.collectionAmount = contractAmount
      this.canBeAdd = contractAmount
    },
    //编辑用
    passONEdit(contractAmount) {
      //可使用金额
      this.canBeAdd = contractAmount
    }
  }
}
</script>
<style scoped lang="less">
.w100 {
  width: 100%;
}
</style>